﻿using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Shatkotha.WPF.Global;
using Shatkotha.WPF.Global.Bases;
using Shatkotha.WPF.Global.Modules;
using Shatkotha.WPF.Global.Modules.Home;
using Shatkotha.WPF.Global.Modules.Contacts;
using Shatkotha.WPF.Global.Modules.SMS;

namespace Shatkotha.WPF.Home.Presenters
{
    public class MenubarPresenter : APresenter, IMenuBarPresenter
    {
        private DelegateCommand<object> _lodeModuleCommand;
        private IRegionManager _regionManager;
        private IUnityContainer _container;
        public IModuleManager ModuleManager { get; set; }

        public DelegateCommand<object> LodeModuleCommand
        {
            get { return _lodeModuleCommand; }
            set { _lodeModuleCommand = value; }
        }

        public MenubarPresenter(IMenuBarView view,IModuleManager moduleManager,IRegionManager regionManager,IUnityContainer container) : base(view)
        {
            Container = container;
            RegionManager = regionManager;
            ModuleManager = moduleManager;
            _lodeModuleCommand = new DelegateCommand<object>(ExecuteLoadModule);
        }

        private void ExecuteLoadModule(object parameter)
        {
            ChangeTool((string)parameter);
        }

        private void ChangeTool(string toolName)
        {
            if (toolName == null)
                return;
            CurrentTool = toolName;
            switch (CurrentTool)
            {
                case Constants.ToolNames.Home:
                    ModuleManager.LoadModule(typeof(IHomeModule).Name);
                    RegionManager.Regions[RegionNames.RegionMain].Activate(Container.Resolve<IHomePresenter>().View);
                    break;
                case Constants.ToolNames.Contacts:
                    ModuleManager.LoadModule(typeof(IContactModule).Name);
                    RegionManager.Regions[RegionNames.RegionMain].Activate(Container.Resolve<IContactPresenter>().View);
                    break;
                case Constants.ToolNames.Sms:
                    ModuleManager.LoadModule(typeof(ISmsModule).Name);
                    RegionManager.Regions[RegionNames.RegionMain].Activate(Container.Resolve<ISmsPresenter>().View);
                    break;
                default:
                    break;
            }
        }

        protected string CurrentTool { get; set; }

        public IRegionManager RegionManager
        {
            get { return _regionManager; }
            set { _regionManager = value; }
        }

        public IUnityContainer Container
        {
            get { return _container; }
            set { _container = value; }
        }
    }
}
